package csc.fresher.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.sql.DataSource;

import csc.fresher.dao.UserDao;
import csc.fresher.entity.User;

public class UserDaoImpl implements UserDao{
	private DataSource dataSource;
	
	@Override
	public void insertUser(User user) {
		/**
		 * Specify the statement
		 */
		String query = "INSERT INTO FORUMS (`first_name`,`last_name`) VALUES (?,?)";
		/**
		 * Define the connection and preparedStatement parameters
		 */
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			/**
			 * Open the connection
			 */
			connection = dataSource.getConnection();
			/**
			 * Prepare the statement
			 */
			preparedStatement = connection.prepareStatement(query);
			/**
			 * Bind the parameters to the PreparedStatement
			 */
			preparedStatement.setString(1, user.getFirstName());
			preparedStatement.setString(2, user.getLastName());
			/**
			 * Execute the statement
			 */
			preparedStatement.execute();
		} catch (SQLException e) {
			/**
			 * Handle any exception
			 */
			e.printStackTrace();
		} finally {
			try {
				/**
				 * Close the preparedStatement
				 */
				if (preparedStatement != null) {
					preparedStatement.close();
				}
				/**
				 * Close the connection
				 */
				if (connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				/**
				 * Handle any exception
				 */
				e.printStackTrace();
			}
		}
	}

}
